<?php
class Page extends Admin_Controller{

	protected $table;
	protected $file_name;

	public function __construct(){
		parent::__construct();
		$this->load->Model(array("Mpage","Mtype","Mcatalogue"));
		$this->table = 'tpt_page';
		$this->file_name = 'page_routes';
		$this->breadcrumbs->push('Page', 'admincp/page/view');
	}
	
	public function view(){
		if($this->input->is_ajax_request()){
            $output = $this->Mpage->listPage();
            $this->output->set_output(json_encode($output));
        }else{
            $data['icon_page']=$this->config->item('icon_view');
            $data['title']='Page Manager';
            $data['sub_title']='Page Manager';
			$this->parser->parse("page/view.tpl", NULL, $data);
        }
	}

	public function add(){
		$data['icon_page']=$this->config->item('icon_create');
        $data['title']='Create New Page';
        $data['sub_title']='Create New Page';
        $data['types']= $this->Mtype->list_all();
        $data['catalogues']= $this->Mcatalogue->list_all();
        $data['parents']= $this->Mpage->getUrlParent();
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$home_menu = 0;
			if(isset($_POST['home_menu'])){
				$home_menu = $this->input->post('home_menu');
			}
			$cata_id = 0;
			if(isset($_POST['cata_id'])){
				$cata_id = $this->input->post('cata_id');
			}
			$data_insert = array(
				'title' => $this->input->post('title'),
				'url_retail' => $this->input->post('url_retail'),
				'url_routes' => $this->input->post('url_routes'),
				'keywords' => $this->input->post('keywords'),
				'description' => $this->input->post('description'),
				'order' => $this->input->post('order'),
				'parent_id'  => $this->input->post('parent_id'),
				'home_menu'  => $home_menu,
				'cata_id'  => $cata_id,
				'created_at' => time(), 
			);
			$this->Mpage->insert($data_insert);
			save_file_setting($this->table, $this->file_name);
			$this->session->set_flashdata(array("success"=>trans('message_create_successfully')));
			redirect(admincp_url()."page/view");		
		}
		$this->breadcrumbs->push('Create Page', 'admincp/page/add');
		$this->parser->parse("page/add.tpl", NULL, $data);
	}

	public function edit(){
		$data['icon_page']=$this->config->item('icon_edit');
        $data['title']='Edit Page';
        $data['sub_title']='Edit Page';
		$cache_id = $id = $this->uri->segment(4);
		$data['types']= $this->Mtype->list_all();
		$data['catalogues']= $this->Mcatalogue->list_all();
		$data['parents']= $this->Mpage->getUrlParent();
		$data['info'] = $this->Mpage->get_data($id);
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$home_menu = 0;
			if(isset($_POST['home_menu'])){
				$home_menu = $this->input->post('home_menu');
			}
			$cata_id = 0;
			if(isset($_POST['cata_id'])){
				$cata_id = $this->input->post('cata_id');
			}
			$data_update = array(
				'title' => $this->input->post('title'),
				'url_retail' => $this->input->post('url_retail'),
				'url_routes' => $this->input->post('url_routes'),
				'keywords' => $this->input->post('keywords'),
				'description' => $this->input->post('description'),
				'order' => $this->input->post('order'),
				'parent_id'  => $this->input->post('parent_id'),
				'home_menu'  => $home_menu,
				'cata_id'  => $cata_id,
				'updated_at' => time(), 
			);
			$this->Mpage->update($data_update,$id);
			save_file_setting($this->table, $this->file_name);
			$this->session->set_flashdata(array("success"=>trans('message_edit_successfully')));
			redirect(admincp_url()."page/view");
		}
		$this->breadcrumbs->push('Edit Page', 'admincp/page/edit/'.$id);
		$this->parser->parse("page/edit.tpl", $cache_id, $data);
	}

	public function delete(){
		$id = $this->uri->segment(4);
		$this->Mpage->delete($id);
		save_file_setting($this->table, $this->file_name);
		$this->session->set_flashdata(array("success"=>trans('message_delete_successfully')));
		redirect(admincp_url()."page");
	}

	public function process(){
		if($this->input->is_ajax_request()){
			$id = $this->input->post("parent_id");
			$data = $this->Mpage->getUrlByParent($id);
			echo json_encode($data);
		}
	}
}